package com.mall4jcola.cloud.gateway;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.mall4jcola.cloud.auth.gateway.UserAuthsChangeGateway;
import com.mall4jcola.cloud.auth.model.UserAuthEntity;
import com.mall4jcola.cloud.tunnel.database.dataobj.UserAuthsDO;
import com.mall4jcola.cloud.tunnel.database.repository.UserAuthsRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 有关登录账户信息表的增删改操作
 * @author hmxchen
 */
@Slf4j
@Service
public class UserAuthsChangeGatewayImpl implements UserAuthsChangeGateway {
    @Resource
    private UserAuthsRepository userAuthsRepository;

    @Override
    public void updateAuthInfo(UserAuthEntity userAuthEntity) {
        Optional<UserAuthsDO> optionalUserAuthsDO = userAuthsRepository.findById(userAuthEntity.getUaId());
        UserAuthsDO authsDO = optionalUserAuthsDO.get();
        //注意jpa不会忽略空值
        BeanUtil.copyProperties(userAuthEntity,authsDO, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        log.info("将要更新的登录信息:{}",authsDO);
        userAuthsRepository.save(authsDO);
    }
}
